package com.jonluo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jonluo.mapper.SysRoleMenuMapper;
import com.jonluo.model.Example;
import com.jonluo.model.SysRoleMenu;
import com.jonluo.service.SysRoleMenuService;
import com.jonluo.utils.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author jonluo
 * Date 2017/9/15
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"roleMenuList", "roleMenuCount"}, allEntries = true)})
    public int save(SysRoleMenu sysRoleMenu) {
        return sysRoleMenuMapper.insertSelective(sysRoleMenu);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"roleMenuList", "roleMenuCount"}, allEntries = true)})
    public int saveBatch(List<SysRoleMenu> list) {
        int i = 0;
        for (SysRoleMenu sysRoleMenu : list){
            i += save(sysRoleMenu);
        }
        return i;
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"roleMenuList", "roleMenuCount"}, allEntries = true),
            @CacheEvict(value = "roleMenu", key = "'roleMenuId'+#p0.id")})
    public int update(SysRoleMenu sysRoleMenu) {
        return sysRoleMenuMapper.updateByPrimaryKeySelective(sysRoleMenu);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"roleMenuList", "roleMenuCount"}, allEntries = true),
            @CacheEvict(value = "roleMenu", key = "'roleMenuId'+#p0")})
    public int delete(String id) {
        return sysRoleMenuMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"roleMenuList", "roleMenuCount","roleMenu"}, allEntries = true)})
    public int deleteBatch(String[] ids) {
        Example example = new Example();
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(ids));
        return sysRoleMenuMapper.deleteByExample(example);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"roleMenuList", "roleMenuCount","roleMenu"}, allEntries = true)})
    public int deleteByRoleIdsOrMenuIds(String[] roleIds,String[] menuIds) {
        Example example = new Example();
        Example.Criteria criteria = example.createCriteria();
        if (menuIds != null){
            criteria.andIn("menuId",Arrays.asList(menuIds));
        }
        if (roleIds != null){
            criteria.andIn("roleId",Arrays.asList(roleIds));
        }
        return sysRoleMenuMapper.deleteByExample(example);
    }

    @Override
    @Cacheable(value = "roleMenu", key = "'roleMenuId'+#p0")
    public SysRoleMenu query(String id) {
        return sysRoleMenuMapper.selectByPrimaryKey(id);
    }

    @Override
    @Cacheable(value = "roleMenuList", key = "'roleMenuList'+#p0.toString()")
    public List<SysRoleMenu> queryList(PageInfo pageInfo) {
        return sysRoleMenuMapper.selectByExample(pageInfo2Example(pageInfo));
    }

    @Override
    @Cacheable(value = "roleMenuCount", key = "'roleMenuCount'+#p0.toString()")
    public int queryTotal(PageInfo pageInfo) {
        return sysRoleMenuMapper.countByExample(pageInfo2Example(pageInfo));
    }

    @Override
    @Cacheable(value = "roleMenuCount", key = "'roleMenuCount'")
    public int queryTotal() {
        return sysRoleMenuMapper.countByExample(null);
    }

    /**
     *
     * @param pageInfo
     * 分页参数转为条件封装example
     * @return example
     */
    private  Example pageInfo2Example(PageInfo pageInfo){
        Example example = new Example();
        Example.Criteria criteria = example.createCriteria();
        //分页
        if (pageInfo.getOffset()!= 0 &&  pageInfo.getOffset()!= 0){
            example.setOffset(pageInfo.getOffset());
            example.setLimit(pageInfo.getLimit());
        }
        //查询条件
        JSONObject json = pageInfo.getJsonParams();
        if (StringUtils.isNotBlank(json.getString("roleId"))){
            criteria.andEqualTo("roleId",json.getString("roleId"));
        }
        return example;
    }
}
